package com.wwz.leetcode;

import org.junit.Test;

public class LeetCode14 {

    public String longestCommonPrefix(String[] strs) {
        if (strs.length < 2) {
            return strs[0];
        }
        String first = strs[0];
        boolean flag = false;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < first.length(); i++) {
            for (int j = 1; j < strs.length; j++) {
                // 防止越界异常
                if (i >= strs[j].length() || first.charAt(i) != strs[j].charAt(i)) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                break;
            }
            sb.append(first.charAt(i));
        }
        return sb.toString();

    }

    @Test
    public void test() {
        System.out.println(longestCommonPrefix(new String[]{"ab", "a"}));
    }

}
